package com.wx.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.github.pagehelper.PageInfo;
import com.wx.common.PageParam;
import com.wx.common.SecurityUser;
import com.wx.common.WrapMapper;
import com.wx.common.Wrapper;
import com.wx.domain.dto.RangeDto;
import com.wx.entity.SysRange;
import com.wx.holder.SecurityUserHolder;
import com.wx.service.SysRangeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;



/**
 * <p>Title:      SysRangeController. </p>
 * <p>Description 范围表 </p>
 * <p>Copyright: Copyright (c) 2016</p>
 * <p>Company:    医百科技有限公司 </p>
 *
 * @author         yangwensheng@100doc.com.cn
 * @since      2021-03-22 10:51:46
 */
@RestController
@RequestMapping(value = "/sysRange", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(tags = "范围表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class SysRangeController {

    @Resource
    private SecurityUserHolder securityUserHolder;

    @Resource
    private SysRangeService sysRangeService;


    @PostMapping("/add")
    @ApiOperation(httpMethod = "POST", value = "新增")
    public Wrapper<?> add(@ApiParam(name = "rangeDto", value = "rangeDto") @RequestBody @Validated RangeDto rangeDto) {
        SecurityUser securityUser = securityUserHolder.getUser();
        sysRangeService.saveRange(rangeDto,securityUser);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE);
    }


    @PostMapping("/delete/{id}")
    @ApiOperation(httpMethod = "POST", value = "根据id删除")
    public Wrapper<?> delete(@PathVariable String id) {
        SecurityUser securityUser = securityUserHolder.getUser();
        int result = sysRangeService.deleteById(id,securityUser.getRealName(),securityUser.getUserId());
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE,result);
    }

    @PostMapping("/update")
    @ApiOperation(httpMethod = "POST", value = "修改")
    public Wrapper<?> update(@ApiParam(name = "rangeDto", value = "rangeDto") @RequestBody @Validated RangeDto rangeDto) {
        SecurityUser securityUser = securityUserHolder.getUser();
        sysRangeService.updateRange(rangeDto,securityUser);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE);
    }



    @PostMapping("/listPage")
    @ApiOperation(httpMethod = "POST", value = "分页列表查询")
    public Wrapper<PageInfo<SysRange>> listPage(@ApiParam(name = "sysRange", value = "sysRange") @RequestBody PageParam<SysRange> pageParam) {
        PageInfo<SysRange> list = sysRangeService.listPage(pageParam);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, list);
    }

    @PostMapping(value = "/export")
    @ApiOperation(httpMethod = "POST", value = "导出范围列表")
    public void export(HttpServletResponse response, @ApiParam(name = "sysRange", value = "sysRange") @RequestBody SysRange sysRange) throws IOException {
        List<SysRange> list = sysRangeService.export(sysRange);
        //添加响应头信息
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("范围列表.xlsx", "UTF-8"));
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        //获取数据
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        //设置背景颜色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        //设置头字体
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short) 15);
        headWriteFont.setBold(true);
        headWriteCellStyle.setWriteFont(headWriteFont);
        //设置头居中
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        //内容策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        //设置 水平居中
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
        EasyExcel.write(response.getOutputStream(), SysRange.class).registerWriteHandler(horizontalCellStyleStrategy)
                .sheet("范围列表").doWrite(list);
    }
}